<template>
  <div class="report">
    <el-row>
      <el-col :span="24">
        <div
          class="grid-content"
          style="min-height: 40px; text-align: center; font-weight: bold"
        >
          <span ali style="font-size: 32px; line-height: 45px" v-text="title" />
        </div>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="8">
        <div class="grid-content bg-blue">
          <div style="margin-left: 12px;font-size: 25px; line-height: 40px; font-weight: 550">
            JOB_NAME
          </div>
          <div  class="card">
            {{ job_name }}
          </div>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content bg-blue">
          <div style="margin-left: 12px;font-size: 25px; line-height: 40px; font-weight: 550">
            BUILD_ID
          </div>
          <div class="card">
            {{ build_id }}
          </div>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content bg-success" :class="[rateFlag > 0 ? 'bg-success' : 'bh-danger']">
          <div style="margin-left: 12px;font-size: 25px; line-height: 40px; font-weight: 550">
            结果
          </div>
          <div
            v-loading="loading"
            v-if="rateFlag === -1"
            style="line-height: 150px"
          ></div>
          <!-- <span v-else-if="rate == 1" style="color: red; font-size: 30px"
            >{{ rate }}%</span
          > -->
          <div v-else class="card">
            {{ rate }}%
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-card>
          <div slot="header" class="title">
            Yapi接口自动化
          </div>
          <!-- <div>{{ yapi }}</div> -->
          <el-table
            v-if="tableDatayapi.length > 0"
            label-class-name="table-header"
            :data="tableDatayapi"
            style="width: 100%; line-height: 20px"
          >
            <el-table-column label="序号" width="70px">
              <template slot-scope="scope">{{ scope.$index + 1 }}</template>
            </el-table-column>
            <el-table-column prop="case_name" label="测试集" width="180" />
            <el-table-column prop="total" label="共执行用例/条" width="180" />
            <el-table-column prop="succ" label="成功" width="180" />
            <el-table-column prop="fail" label="失败" width="180" />
            <el-table-column prop="err" label="异常" width="180" />
            <el-table-column prop="info" label="查看详情" width="240">
              <template slot-scope="scope">
                <el-row v-if="scope.row.total === 0"> 正在执行中 </el-row>
                <el-link @click="See(scope.row.info)" v-else class="link" :title="scope.row.info">{{
                  scope.row.info
                }}</el-link>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-card>
          <!-- <div class="grid-content" style="min-height: 30px"> -->
            <div slot="header" class="title">
              AppUI
            </div>
            <!-- <div>{{ app_ui }}</div> -->
            <el-table
              v-if="tableDataappui.length > 0"
              label-class-name="table-header"
              :data="tableDataappui"
              style="width: 100%; line-height: 20px"
            >
              <el-table-column label="序号" width="70px">
                <template slot-scope="scope">{{ scope.$index + 1 }}</template>
              </el-table-column>
              <el-table-column prop="case_name" label="测试集" width="180" />
              <el-table-column prop="total" label="共执行用例/条" width="180" />
              <el-table-column prop="succ" label="成功" width="180" />
              <el-table-column prop="fail" label="失败" width="180" />
              <el-table-column prop="err" label="异常" width="180" />
              <el-table-column prop="info" label="查看详情" width="240">
                <template slot-scope="scope">
                  <el-row v-if="scope.row.total === 0"> 正在执行中 </el-row>
                  <el-link @click="See(scope.row.info)" v-else class="link" :title="scope.row.info">{{
                    scope.row.info
                  }}</el-link>
                </template>
              </el-table-column>
            </el-table>
          <!-- </div> -->
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-card>
          <!-- <div class="grid-content" style="min-height: 30px"> -->
            <div slot="header" class="title">
              WebUI
            </div>
            <!-- <div>{{ web_ui }}</div> -->
            <el-table
              v-if="tableDatawebui.length > 0"
              label-class-name="table-header"
              :data="tableDatawebui"
              style="width: 100%; line-height: 20px"
            >
              <el-table-column label="序号" width="70px">
                <template slot-scope="scope">{{ scope.$index + 1 }}</template>
              </el-table-column>
              <el-table-column prop="case_name" label="测试集" width="180" />
              <el-table-column prop="total" label="共执行用例/条" width="180" />
              <el-table-column prop="succ" label="成功" width="180" />
              <el-table-column prop="fail" label="失败" width="180" />
              <el-table-column prop="err" label="异常" width="180" />
              <el-table-column prop="info" label="查看详情" width="240">
                <template slot-scope="scope">
                  <el-row v-if="scope.row.total === 0"> 正在执行中 </el-row>
                  <el-link @click="See(scope.row.info)" v-else class="link" :title="scope.row.info">{{
                    scope.row.info
                  }}</el-link>
                </template>
              </el-table-column>
            </el-table>
          <!-- </div> -->
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { Message } from "element-ui";
import { selCiReport } from "@/api/ciReport";

export default {
  name: "CiReport",
  data() {
    return {
      title: "集成报告",
      loading: true,
      build_id: "",
      job_name: "",
      rate: 0,
      rateFlag: 0,
      app_ui: "",
      web_ui: "",
      yapi: "",
      tableDatayapi: {},
      tableDataappui: {},
      tableDatawebui: {},
      timer: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    See(e) {
      window.open(e);
    },
    getList() {
      
      this.build_id = this.getUrlKey("build_id");
      this.task_id = this.getUrlKey("task_id");
      if (
        this.build_id !== null &&
        this.build_id !== "" &&
        this.task_id !== "" &&
        this.task_id !== ""
      ) {
        selCiReport(this.task_id, this.build_id).then((response) => {
          if (response.code === 20000) {
            this.job_name = response.data.job_name;
            this.rate = parseFloat(response.data.rate * 100).toFixed(2);
            this.app_ui = response.data.app_ui;
            this.web_ui = response.data.web_ui;
            this.yapi = response.data.yapi;
            this.tableDatayapi = response.data.yapi;
            this.tableDataappui = response.data.app_ui;
            this.tableDatawebui = response.data.web_ui;
            if (response.data.rate === -1) {
              this.rateFlag = -1;
              clearTimeout(this.timer);
              this.timer = setTimeout(() => {
                //设置延迟执行
                this.getList();
              }, 5 * 1000);
            } else if (response.data.rate < 0.6) {
              this.rateFlag = 0;
            } else {
              this.rateFlag = 1
            }
          } else {
            Message.error(response.Message);
          }
        });
      } else {
        Message.error("访问地址参数异常");
      }
    },
    getUrlKey(name) {
      return (
        decodeURIComponent(
          (new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(
            location.href
          ) || [, ""])[1].replace(/\+/g, "%20")
        ) || null
      );
    },
  },
};
</script>
<style>
/*表头太高，修改此样式*/
.el-main {
  /*background-color: #eeeeee;*/
  line-height: 14px;
}

.report {
  width: 95%;
  height: 100%;
  margin: 10px 40px;
  position: relative;
}

.el-row {
  margin-bottom: 20px;
}

.el-col {
  border-radius: 4px;
}
.title{
  font-size: 25px;
  line-height: 30px;
  font-weight: 10;
}
.card{
  font-size: 30px;
  line-height: 60px;
  text-align: center;
  font-weight: bold;
  color: #fff;
}
.link span{
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2 !important;
  -webkit-box-orient: vertical;
}
.el-card__body{
  padding-top: 0 !important;
}
.bg-purple-dark {
  background: #fcfdec;
}

.bg-purple {
  background: #fcfdec;
}

.bg-purple-light {
  background: #fcfdec;
}
.bg-blue{
  background: #409EFF;
}

.bg-success{
  background: #67C23A;
}
.bg-danger{
  background: #F56C6C;
}
.grid-content {
  border-radius: 4px;
  min-height: 150px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
